﻿using Heroius.Extension;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Heroius.Extension.WPF;
using Heroius.Extension.Desktop;

namespace PicWaterMarker
{
    /// <summary>
    /// 为 <see cref="ImageHandler"/> 提供的配置选项，应在程序启动时第一时间创建并为<see cref="ImageHandler.Configuration"/> 赋值。
    /// </summary>
    public class ImageHandlerConfiguration: ObservableEntity
    {
        /// <summary>
        /// 图片的位置
        /// </summary>
        [PropertyPanel(Label ="图片路径", Order = 0, Type = PropertyPanelSupportedType.File), PropertyPanelFileAttribute(Filter = "位图|*.jpg;*.png;*.bmp")]
        public string PicturePath { get; set; } = string.Empty;
        /// <summary>
        /// 图片的位置
        /// </summary>
        [PropertyPanel(Label = "保存路径", Order = 0, Type = PropertyPanelSupportedType.FileSaving), PropertyPanelFileAttribute(Filter = "位图|*.jpg;*.png;*.bmp")]
        public string TargetPath { get; set; } = string.Empty;

        /// <summary>
        /// 指示是否启动水印，启用后会在返回图片上自动添加水印
        /// </summary>
        [PropertyPanel(Label = "添加水印", Order = 1)]
        public bool ImageWatermark { get; set; } = true;
        /// <summary>
        /// 水印图片的位置，相对于程序根目录的路径
        /// </summary>
        [PropertyPanel(Label = "水印路径", Order = 1, Type = PropertyPanelSupportedType.File), PropertyPanelFileAttribute(Filter = "位图|*.jpg;*.png;*.bmp")]
        public string WatermarkPicturePath { get; set; } = string.Empty;

        /// <summary>
        /// 是否启用图片缩放
        /// <para>使用配置的尺寸限制对原始图片进行自动缩放</para>
        /// </summary>
        [PropertyPanel(Label ="是否进行缩放，等比例地", Order = 2)]
        public bool ImageScale { get; set; } = false;
        /// <summary>
        /// 当<see cref="ImageScale"/> 为真，限定响应图片的宽度上限
        /// </summary>
        [PropertyPanel(Label = "缩放后宽度限制", Order = 2)]
        public int ImageScale_Width { get; set; } = 500;
        /// <summary>
        /// 当<see cref="ImageScale"/> 为真，限定响应图片的高度上限
        /// </summary>
        [PropertyPanel(Label = "缩放后高度限制", Order = 2)]
        public int ImageScale_Height { get; set; } = 500;

    }

    /// <summary>
    /// 图片请求处理器
    /// </summary>
    public class ImageHandler
    {
        public void ProcessRequest(ImageHandlerConfiguration Configuration)
        {
            System.Drawing.Image TargetImage;
            string ext;
            if (File.Exists(Configuration.PicturePath))
            {
                TargetImage = Image.FromFile(Configuration.PicturePath);
                ext = new FileInfo(Configuration.TargetPath).Extension.ToLower();
                if (Configuration.ImageScale || Configuration.ImageWatermark)
                {
                    if (Configuration.ImageScale)
                    {
                        double wfactor = (TargetImage.Width > Configuration.ImageScale_Width) ? (Convert.ToDouble(Configuration.ImageScale_Width) / Convert.ToDouble(TargetImage.Width)) : 1;
                        double hfactor = (TargetImage.Height > Configuration.ImageScale_Height) ? (Convert.ToDouble(Configuration.ImageScale_Height) / Convert.ToDouble(TargetImage.Height)) : 1;
                        double factor = Math.Min(wfactor, hfactor);
                        int toWidth = Convert.ToInt32(TargetImage.Width * factor);
                        int toHeight = Convert.ToInt32(TargetImage.Height * factor);
                        var Source = TargetImage;
                        TargetImage = new Bitmap(toWidth, toHeight);
                        Graphics g = Graphics.FromImage(TargetImage);
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.Clear(Color.Transparent);
                        g.DrawImage(Source, new Rectangle(0, 0, toWidth, toHeight), new Rectangle(0, 0, Source.Width, Source.Height), GraphicsUnit.Pixel);
                        g.Dispose();
                    }
                    if (Configuration.ImageWatermark)
                    {
                        Graphics g = Graphics.FromImage(TargetImage);
                        Image watermark = Image.FromFile(Configuration.WatermarkPicturePath);

                        double wfactor = Convert.ToDouble(TargetImage.Width) * 0.5 / Convert.ToDouble(watermark.Width);
                        double hfactor = Convert.ToDouble(TargetImage.Height) * 0.3 / Convert.ToDouble(watermark.Height);
                        double factor = Math.Min(wfactor, hfactor);
                        int toWidth = Convert.ToInt32(watermark.Width * factor);
                        int toHeight = Convert.ToInt32(watermark.Height * factor);
                        g.DrawImage(watermark, new Rectangle(TargetImage.Width - toWidth, TargetImage.Height - toHeight, toWidth, toHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel);
                        watermark.Dispose();
                        g.Dispose();
                    }
                }
            }
            else
            {
                throw new Exception("未选择任何功能");
            }

            switch (ext)
            {
                case ".jpg":
                default:
                    TargetImage.Save(Configuration.TargetPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;
                case ".png":
                    TargetImage.Save(Configuration.TargetPath, System.Drawing.Imaging.ImageFormat.Png);
                    break;
                case ".bmp":
                    TargetImage.Save(Configuration.TargetPath, System.Drawing.Imaging.ImageFormat.Bmp);
                    break;
            }
            TargetImage.Dispose();
        }
    }
}
